<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="$t('msgCenter.feedBack.feedbackDate')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('msgCenter.feedBack.informationStatus')">
          <el-select v-model="searchForm.solve" clearable :placeholder="$t('msgCenter.feedBack.promptStatus')">
            <el-option
              v-for="item in $t('msgCenter.feedBack.solve')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('msgCenter.feedBack.userAccount')">
          <el-input v-model="searchForm.phoneNumber" clearable :placeholder="$t('msgCenter.feedBack.promptAccount')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%"
      >
        <el-table-column
          fixed="left"
          prop="createTime"
          :label="tableText['feedbackDate']"
          min-width="170"
        />
        <el-table-column
          fixed="left"
          prop="phoneNumber"
          :label="tableText['userAccount']"
          min-width="150"
        />
        <el-table-column
          prop="title"
          :label="tableText['feedbackTopic']"
          min-width="100"
        />
        <el-table-column
          prop="content"
          :label="tableText['feedbackContent']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ (scope.row.content && scope.row.content.length > 20) ? scope.row.content.substring(0,10) + '...' : scope.row.content }}
          </template>
        </el-table-column>
        <el-table-column
          prop="appVersion"
          :label="tableText['versionNumber']"
          min-width="120"
        />
        <el-table-column
          prop="solve"
          :label="tableText['whetherSolve']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row.solve !== null ? setValue(scope.row.solve, 'solve') : '' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="remarks"
          :label="tableText['remarks']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ (scope.row.remarks && scope.row.remarks.length > 20) ? scope.row.remarks.substring(0,10) + '...' : scope.row.remarks }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="120"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="editItem(scope.row)">
              {{ $t('common.clickShow') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <feed-back-dialog
      :show-dialog="editDialog"
      :choose-item="chooseItem"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
  </el-row>
</template>

<script>
import { getFeedBack } from '@/api/operation'
import { formatDate } from '@/utils/method'
import FeedBackDialog from '@/components/operation/feed-back-dialog'
export default {
  name: 'FeedBack',
  components: {
    FeedBackDialog
  },
  data() {
    return {
      loading: false,
      editDialog: false,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      time: null,
      searchForm: this.params(),
      chooseItem: {
        id: null,
        createTime: null,
        appVersion: null,
        title: null,
        content: null,
        images: null,
        solve: null,
        remarks: null
      }
    }
  },
  mounted() {
    this.tableText = this.$t('msgCenter.feedBack')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        startDate: null,
        endDate: null,
        solve: '',
        phoneNumber: null
      }
    },
    setValue(value, data) {
      const map = this.$t(`msgCenter.feedBack.${data}`)
      const name = map.filter(item => {
        return item.value.toString() === value.toString()
      })
      return (name.length > 0 && name[0].desc) ? name[0].desc : ''
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = null
      this.getData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['startDate'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['endDate'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['startDate'] = null
        data['endDate'] = null
      }
      return data
    },
    getData() {
      this.loading = true
      getFeedBack(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    changeDialog(state) {
      this.editDialog = state
    },
    editSuccess() {
      this.choose({})
      this.getData()
    },
    editItem(item) {
      this.choose(item)
      this.editDialog = true
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.createTime = param.createTime
      this.chooseItem.appVersion = param.appVersion
      this.chooseItem.title = param.title
      this.chooseItem.content = param.content
      this.chooseItem.images = param.images
      this.chooseItem.solve = param.solve
      this.chooseItem.remarks = param.remarks
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 10px;
}
</style>
